﻿
using System;
using System.Xml;
using Microsoft.SharePoint.WebPartPages;

namespace SharePoint.Orm.Helpers {
	///<summary>
	/// Helper methods for working with <see cref="ContentEditorWebPart"/>.
	///</summary>
	public static class ContentEditorWebPartHelper {
		///<summary>
		/// Converts the given html into a format suitable for the <see cref="ContentEditorWebPart.Content"/>.
		///</summary>
		///<param name="html">The raw html.</param>
		///<returns>The content.</returns>
		///<exception cref="NullReferenceException">If the XML document can't be created.</exception>
		public static XmlElement ToContent(string html) {
			XmlDocument doc = new XmlDocument();
			doc.LoadXml("<content/>");
			if (doc.DocumentElement == null) {
				throw new NullReferenceException("could not create XML document");
			}
			doc.DocumentElement.AppendChild(doc.CreateCDataSection(html));
			return doc.DocumentElement;
		}
	}
}
